﻿using System;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;

namespace Medianamik.Core.Sugar
{
    public static class Resources
    {
        public static void RegisterScript(string pResourceName, Page pPage)
        {
            RegisterScript(pResourceName, pPage, pPage.GetType().Assembly);

        }
        public static void RegisterScript(string pResourceName, Page pPage, Assembly pAssembly)
        {
            EnsureManifestResourceExist(pResourceName, pAssembly);

            ScriptReference sr = new ScriptReference(pAssembly.GetName().Name + "." + pResourceName, pAssembly.GetName().Name);
            ScriptManager m = ScriptManager.GetCurrent(pPage);
            m.Scripts.Add(sr);
        }

        public static void RegisterCSS(string pResourceName, Page pPage, Type pType)
        {
            RegisterCSS(pResourceName, pPage, pType, pPage.GetType().Assembly);
        }
        public static void RegisterCSS(string pResourceName, Page pPage, Type pType, Assembly pAssembly)
        {
            EnsureManifestResourceExist(pResourceName, pAssembly);

            string tempLink = "<link rel='stylesheet' text='text/css' href='{0}' />";
            string location = pPage.ClientScript.GetWebResourceUrl(pType, pAssembly.GetName().Name + "." + pResourceName);
            LiteralControl include = new LiteralControl(String.Format(tempLink, location));
            ((HtmlHead)pPage.Header).Controls.Add(include);

        }

        public static string GetWebResourceUrl(string pResourceName, Page pPage, Type pType)
        {
            return GetWebResourceUrl(pResourceName, pPage, pType, pPage.GetType().Assembly);
        }

        public static string GetWebResourceUrl(string pResourceName, Page pPage, Type pType, Assembly pAssembly)
        {
            EnsureManifestResourceExist(pResourceName, pAssembly);
            string location = pPage.ClientScript.GetWebResourceUrl(pType, pAssembly.GetName().Name + "." + pResourceName);
            return location;
        }

        public static StreamReader GetResourceReader(string pResourceName, Page pPage)
        {
            return GetResourceReader(pResourceName, pPage.GetType().Assembly);

        }
        public static StreamReader GetResourceReader(string pResourceName, Assembly pAssembly)
        {
            EnsureManifestResourceExist(pResourceName, pAssembly);
            System.IO.StreamReader reader = new System.IO.StreamReader(pAssembly.GetManifestResourceStream(pAssembly.GetName().Name + "." + pResourceName));
            return reader;
        }

        public static string GetResourceText(string pResourceName, Page pPage)
        {
            return GetResourceText(pResourceName, pPage.GetType().Assembly);

        }
        public static string GetResourceText(string pResourceName, Assembly pAssembly)
        {
            EnsureManifestResourceExist(pResourceName, pAssembly);          
            using (StreamReader reader = GetResourceReader(pResourceName, pAssembly))
            {
                return reader.ReadToEnd();
            }
            
        }

        private  static void EnsureManifestResourceExist(string pResourceName, Assembly pAssembly)
        {
            if (!ManifestResourceExist(pResourceName, pAssembly))
            {
                string message = "Couldn't find embedded resource " + pResourceName + " in assembly " + pAssembly.FullName;
                throw new CoreException(message);
            }
        }
        public static bool ManifestResourceExist(string pResourceName, Assembly pAssembly)
        {   //NOTE: If resource is located in subfolder of C# project, path of subfolder should be specified (it is included as part of namespace)
            //' Resources are named using a fully qualified name ((including namespace).

            ManifestResourceInfo info = pAssembly.GetManifestResourceInfo(pAssembly.GetName().Name + "." + pResourceName);
            bool exist = info != null;
            return exist;
        }
    }
}